<script setup>
import { ref } from 'vue'
import 'element-plus/theme-chalk/el-message.css'
import { artAddChannelService } from '@/api/article'
import { artEditChannelService } from '@/api/article'
import { ElMessage } from 'element-plus'

const dialogVisible = ref(false)
const open = async (row) => {
  dialogVisible.value = true
  formModel.value = { ...row } //添加（重置）编辑（存储数据）
}
defineExpose({
  open,
})
const formModel = ref({
  cate_name: '',
  cate_alias: '',
})
const rules = {
  cate_name: [
    { required: true, message: '请输入分类名称', trigger: 'blur' },
    {
      pattern: /^\S{1,10}$/,
      message: '分类名必须是1-10位的非空字符',
      trigger: 'blur',
    },
  ],
  cate_alias: [
    { required: true, message: '请输入分类别名', trigger: 'blur' },
    {
      pattern: /^[a-zA-Z0-9]{1,15}$/,
      message: '分类别名必须是1-15位的字母数字',
      trigger: 'blur',
    },
  ],
}
const onSubmit = async () => {
  await formRef.value.validate()
  // console.log(formModel.value);
  if (!formModel.value.id) {
    //add
    await artAddChannelService(formModel.value)
  } else {
    //edit
    await artEditChannelService(formModel.value)
  }
  ElMessage({
    type: 'success',
    message: formModel.value.id ? '编辑成功' : '添加成功',
  })
  dialogVisible.value = false

  emit('success')
}
const emit = defineEmits(['success'])
const formRef = ref()
</script>
<template>
  <el-dialog :title="!formModel.id ? '添加弹层' : '编辑弹层'" v-model="dialogVisible" width="30%">
    <el-form :model="formModel" style="padding-right: 30px" :rules="rules" ref="formRef">
      <el-form-item label="分类名称：" prop="cate_name">
        <el-input placeholder="请输入分类名称" v-model="formModel.cate_name"></el-input>
      </el-form-item>
      <el-form-item label="分类别名：" prop="cate_alias">
        <el-input placeholder="请输入分类别名" v-model="formModel.cate_alias"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="onSubmit">确 定</el-button>
      </span>
    </template>
  </el-dialog>
</template>
<style scoped></style>
